package cn.lyp.controller;

import cn.lyp.domain.Order;
import cn.lyp.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;

    @LoadBalanced
    @GetMapping("/{id}")
    private Order get(@PathVariable("id") Long id) {
        /**
         * 发起远程调用user-server 微服务 获取一个user对象
         *             发送http请求调用：http://localhost:10200/user/1
         *             导包 httpclient
         */

        String url = "http://user-server/user/1";
        User user = restTemplate.getForObject(url, User.class);

        return new Order(id, "2022112516218", user);
    }

    public Order userFallback(@PathVariable("id") Long id) {
        return new Order(id, "服务不可用，请稍后重试",null);
    }
}
